import { loginService } from '@/api/auth'
import { defineStore } from 'pinia';
import { ref, computed } from 'vue';

// 创建 Pinia 存储
export const useAuthStore = defineStore('auth', () => {
	// 状态
	const token = ref(null)
	const user = ref(null)

	// Getter（计算属性）
	const isAuthenticated = computed(() => !!token.value);

	// Action（登录）
	const login = async (email, password) => {
		const data = await loginService(email, password)
		token.value = data.token
		user.value = data.user
	}

	// Action（登出）
	const logout = () => {
		token.value = null
		user.value = null
	}

	return { token, user, isAuthenticated, login, logout };
}, {
	persist: true // 启用持久化
})